package com.linewow.xhyy.nihaonew.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.activity.UserMineActivity;
import com.linewow.xhyy.nihaonew.adapter.FollowAdapter;
import com.linewow.xhyy.nihaonew.adapter.FollowFanUserAdapter;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.base.BaseFragment;
import com.linewow.xhyy.nihaonew.base.BaseInfo;
import com.linewow.xhyy.nihaonew.entity.NiHaoEntity;
import com.linewow.xhyy.nihaonew.entity.user.SimpleUserInfo;
import com.linewow.xhyy.nihaonew.entity.user.Uinfo;
import com.linewow.xhyy.nihaonew.model.HttpModel;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;
import com.linewow.xhyy.nihaonew.util.NewsCallback;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/10/12.
 *
 */
public class FollowFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener {
    @Bind(R.id.follow_recycler)
    RecyclerView followRecycler;
    private String TAG="FollowFragment";
    private FollowAdapter adapter;
    private FollowFanUserAdapter userAdapter;//他人和本人 分离
    private String uid="";
    private int page;


    @Override
    public View initView() {
        View view = View.inflate(getActivity(), R.layout.fragment_follow, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        initRecycler();
        Bundle bundle=getArguments();
        if(bundle!=null){
            Uinfo uinfo=bundle.getParcelable("info");
            if(uinfo!=null){
                uid=uinfo.uid;
            }
            Log.e(TAG,"uid的值444"+uid);
        }
        requestInfo();

        followRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                SimpleUserInfo info=null;
                if(uid.equals("")){
                    info=adapter.getData().get(i);
                }else{
                    info=userAdapter.getData().get(i);
                }
                startUserActivity(info);
//                Log.e("FollowFragment","点击了");
            }
        });
        followRecycler.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void SimpleOnItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                int id=view.getId();
                Log.e("FollowFragment","点击了");
                switch (id){
                    case R.id.item_follow_follow_cancel:
                        Log.e("FollowFragment","取消关注了");
                        SimpleUserInfo simpleUserInfo=adapter.getData().get(i);
                        OkHttpUtils.get()
                                .url(HttpModel.USERUFOLLOW)
                                .addParams("touid",simpleUserInfo.uid)
                                .addParams("token", NiHaoApp.token)
                                .build()
                                .execute(new NewsCallback<NiHaoEntity>(NiHaoEntity.class) {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {
                                        Log.e(TAG,"网络请求失败"+e.getMessage());
                                    }

                                    @Override
                                    public void onResponse(NiHaoEntity response, int id) {
                                        Log.e(TAG,"取消结果返回"+response);
                                        if(response.status){
                                            page=0;
                                            requestInfo();
                                        }
                                    }
                                });

                        break;
                }

            }
        });
    }

    private void startUserActivity(SimpleUserInfo info) {
        Uinfo uinfo=new Uinfo();
        uinfo.uid=info.uid;
        Intent intent=new Intent(FollowFragment.this.getActivity(), UserMineActivity.class);
        Bundle bundle=new Bundle();
        bundle.putParcelable("info",uinfo);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    private void initRecycler() {
        LinearLayoutManager manager=new LinearLayoutManager(FollowFragment.this.getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        followRecycler.setItemAnimator(new DefaultItemAnimator());
        followRecycler.setLayoutManager(manager);

    }


    private void requestInfo() {
        NiHaoOkgo.followListInfo(uid,page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseInfo<SimpleUserInfo>, List<SimpleUserInfo>>() {
                    @Override
                    public List<SimpleUserInfo> call(BaseInfo<SimpleUserInfo> simpleUserInfoBaseInfo) {
                        return simpleUserInfoBaseInfo.data;
                    }
                })
                .subscribe(new Action1<List<SimpleUserInfo>>() {
                    @Override
                    public void call(List<SimpleUserInfo> simpleUserInfos) {
                        Log.e(TAG,"大小"+simpleUserInfos.size());
                        if(uid.equals("")){
                            if(adapter==null){
                                adapter=new FollowAdapter(simpleUserInfos);
                                adapter.isFirstOnly(false);
                                adapter.setOnLoadMoreListener(FollowFragment.this);
                                followRecycler.setAdapter(adapter);
                            }else{
                                if(page==0){
                                    adapter.setNewData(simpleUserInfos);

                                }else{
                                    adapter.addData(simpleUserInfos);
                                    if(simpleUserInfos.size()<15){
                                        adapter.loadComplete();
                                    }
                                }
                            }
                        }else{
                            if(userAdapter==null){
                                userAdapter=new FollowFanUserAdapter(simpleUserInfos);
                                userAdapter.setFollowFlag(true);
                                userAdapter.isFirstOnly(false);
                                userAdapter.setOnLoadMoreListener(FollowFragment.this);
                                followRecycler.setAdapter(userAdapter);
                            }else{
                                userAdapter.addData(simpleUserInfos);
                                if(simpleUserInfos.size()<15){
                                    userAdapter.loadComplete();
                                }
                            }
                        }


                    }
                });
    }

    @Override
    public void initList() {
        super.initList();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        requestInfo();
    }
}
